package com.molin;

import com.molin.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/listStudentServlet")
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.从文件读取数据，创建Student对象并存储到集合中去
        BufferedReader br=new BufferedReader(new FileReader("D:\\stu.txt"));
        ArrayList<Student> studentArrayList=new ArrayList<>();
        String line;
        while ((line=br.readLine())!=null){
            //e.g. zs,23,100
            String[] split = line.split(",");
            studentArrayList.add(new Student(split[0],Integer.parseInt(split[1]),Integer.parseInt(split[2])));
        }
        br.close();

        //2.添加完毕后，将学生集合放到会话域中
        req.getSession().setAttribute("studentArrayList",studentArrayList);
        //3.跳转到显示学生页面
        resp.sendRedirect("/stu/listStudent.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
